package com.cscp.javastudy.juc.lock;

/**
 * 卖票
 */
public class TicketSaleDemo1 {

    public static void main(String[] args) {

        Ticket ticket = new Ticket();
        // (参数) -> {代码...}
        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.sale();
            }
        }, "C").start();
    }


}

/**
 * 资源类
 */
class Ticket {
    // 属性
    private int number = 50;
    // 方法
    public synchronized void sale(){
        if (number>0){
            System.out.println(Thread.currentThread().getName()+" 卖出了 " + (number--) + " 票，剩余：" + number);
        }
    }
}
